package com.hghivln.blog.common.lang;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * 统一结果封装
 */
@Data
@AllArgsConstructor
public class Result<T> implements Serializable {

    private int code;
    private String msg;
    private T data;
    private Long count = 0L;

    /**
     * 默认返回 SUCCESS(20000, "操作成功！"),
     */
    public Result() {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMsg();
    }

    /**
     * 返回指定文本
     */
    public Result(String msg) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = msg;
    }

    /**
     * 返回数据集
     */
    public Result(T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMsg();
        this.data = data;
    }

    /**
     * 返回指定文本和数据集
     */
    public Result(String msg, T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = msg;
        this.data = data;
    }

    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public Result(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    public Result(ResultEnum resultEnum, String msg) {
        this.code = resultEnum.getCode();
        this.msg = msg;
    }

    public Result(Long count, T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMsg();
        this.data = data;
        this.count = count;
    }

    /**
     * 专门返回错误结果
     */
    public static Result<?> error() {
        return new Result<>(ResultEnum.ERROR);
    }

    public static Result<?> ok() {
        return new Result<>();
    }
}
